<?php
namespace app\admin\controller;

class Article extends Adm
{
	use \app\admin\traits\Curd;
    public function index()
    {
    	return $this->fetch();
    }
    protected function _add()
    {
        $cates = \app\admin\model\cate::select()->toArray();
        $cates = $this->listtotreearr($cates,'parent_id');
        $this->assign(get_defined_vars());
    }
    private function listtotreearr(
        $lists,
        $parent_field='parent_id',
        $parent_id=0,
        $son_field='children'
    )
    {
        $list = [];
        $rows = $lists;
        foreach($rows as $k=>$v)
        {
            $data = $v;
            $data['children'] = [];
            if($v['parent_id']==$parent_id)
            {
                unset($lists[$k]);
                $sons = $this->listtotreearr($lists,$parent_field,$v['id']);
                foreach($sons as $i=>$item){
                    array_push($data['children'], $item);
                }
                array_push($list, $data);
            }
        }
        return $list;
    }
}
